<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <!-- 放置页签 -->
          <el-tab-pane label="角色管理">
            <!-- 新增角色按钮 -->
            <el-row style="height: 60px">
              <el-button
                icon="el-icon-plus"
                size="small"
                type="primary"
                @click.native="addrole"
              >新增角色</el-button>
            </el-row>
            <!-- 表格 -->
            <el-table border="" :data="rolelist.rows">
              <el-table-column label="序号" width="120" type="index" />
              <el-table-column label="角色名称" width="240" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="success" @click="upsetting(row.id)">分配权限</el-button>
                  <el-button
                    size="small"
                    type="primary"
                    @click.native="editRole(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="danger"
                    @click.native="delRole(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页组件 -->
            <el-row
              type="flex"
              justify="center"
              align="middle"
              style="height: 60px"
            >
              <!-- 分页组件 -->
              <el-pagination
                layout="prev,pager,next"
                :total="rolelist.total"
                :page-size="pagesetting.pagesize"
                :current-page="pagesetting.page"
                @current-change="currentchange"
              />
            </el-row>
          </el-tab-pane>
          <el-tab-pane label="公司信息">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <el-form label-width="120px" style="margin-top: 50px" disabled>
              <el-form-item label="公司名称">
                <el-input
                  v-model="company.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="company.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="company.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="company.remarks"
                  type="textarea"
                  :rows="3"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>

    <el-dialog :title="roleForm.id?'编辑角色':'新增角色'" :visible="showDialog" @close="btnCancel">
      <el-form
        ref="roleForm"
        :model="roleForm"
        :rules="rules"
        label-width="120px"
      >
        <el-form-item label="角色名称" prop="name">
          <el-input v-model="roleForm.name" />
        </el-form-item>
        <el-form-item label="角色描述" prop="description">
          <el-input v-model="roleForm.description" />
        </el-form-item>
      </el-form>
      <!-- 底部 -->
      <el-row slot="footer" type="flex" justify="center">
        <el-col :span="6">
          <el-button size="small" @click="btnCancel">取消</el-button>
          <el-button size="small" type="primary" @click="btnOK">确定</el-button>
        </el-col>
      </el-row>
    </el-dialog>
    <el-dialog :visible="updialog" @close="outlog">
      <el-checkbox-group v-model="upSetting">
        <el-checkbox v-for="item in settings" :key="item.id" :label="item.id">
          {{ item.name }}
        </el-checkbox>
      </el-checkbox-group>
      <span slot="footer">
        <el-button size="small" @click="outlog">取消</el-button>
        <el-button
          size="small"
          type="primary"
          @click="upSet"
        >确定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { company, rolelist, delrole, addrole, getrole, editrole, upset } from '@/api/setting'
import { getPermissionList } from '@/api/permission'
export default {
  data() {
    return {
      company: {},
      rolelist: {},
      pagesetting: {
        pagesize: 5,
        page: 1
      },
      roleForm: {
        name: '',
        description: ''
      },
      showDialog: false,
      rules: {
        name: [{ required: true, message: '角色名是必填的', trigger: 'blur' }],
        description: [{ min: 0, max: 50, message: '描述要在50个字符以内', trigger: 'blur' }]
      },
      updialog: false,
      upSetting: [],
      settings: [],
      settingsId: ''
    }
  },
  async  created() {
    this.getrolelist()
    this.settings = (await getPermissionList()).data
  },
  methods: {
    async upsetting(id) {
      this.settingsId = id
      this.upSetting = (await getrole(id)).data.permIds
      this.updialog = true
      console.log(this.upSetting)
    },
    outlog() {
      this.upSetting = []
      this.settingsId = ''
      this.updialog = false
    },
    async upSet() {
      await upset({ id: this.settingsId, permIds: this.upSetting })
      this.$message.success('更改成功')
      this.outlog()
    },
    async getrolelist() {
      this.company = (await company(this.$store.state.user.userinfo.companyId)).data
      this.rolelist = (await rolelist(this.pagesetting)).data
    },
    currentchange(val) {
      this.pagesetting.page = val
      this.getrolelist()
    },
    async delRole(id) {
      await this.$confirm('要删除吗？')
      delrole(id).catch(() => { })
      this.getrolelist()
    },
    addrole() {
      this.showDialog = true
    },
    btnCancel() {
      this.roleForm = {
        name: '',
        description: ''
      }
      this.$refs.roleForm.resetFields()
      this.showDialog = false
    },
    async btnOK() {
      await this.$refs.roleForm.validate()
      if (this.roleForm.id) {
        await editrole(this.roleForm.id, this.roleForm)
      } else {
        await addrole(this.roleForm)
      }
      this.$message.success('操作成功')
      this.getrolelist()
      this.btnCancel()
    },
    async editRole(id) {
      this.roleForm = (await getrole(id)).data
      this.showDialog = true
    }
  }
}
</script>

<style>
</style>
